import PopupBase from "../../../eazax-ccc/components/popups/PopupBase";
import { CCEasing } from "../../../eazax-ccc/components/tweens/CCEasing";
import { ResPopupOptions } from "./ResPopup";

const { ccclass, property } = cc._decorator;

/** 资源弹窗 */
@ccclass
export default class Dialog extends PopupBase<ResPopupOptions> {

    @property(cc.Node)
    private closeBtn: cc.Node = null;



    /** 资源弹窗路径 */
    public static get path() { return 'prefabs/cases/dialog'; }

    protected onLoad() {
        this.registerEvent();
    }

    protected onDestroy() {
        this.unregisterEvent();
    }

    private registerEvent() {
        this.closeBtn.on(cc.Node.EventType.TOUCH_END, this.onCloseBtnClick, this);
    }

    private unregisterEvent() {
        this.closeBtn.off(cc.Node.EventType.TOUCH_END, this.onCloseBtnClick, this);
    }

    protected updateDisplay(options: ResPopupOptions) {
        console.log(options);

    }
    private timeScale = 2;

    /**
     * 展示弹窗动画（子类请重写此函数以实现自定义逻辑）
     */
    public async beforeShow(): Promise<any> {
        return new Promise((resolve, reject) => {

            // 播放背景动画
            cc.tween(this.background)
                .to(this.animTime * 0.8 * this.timeScale, { opacity: 200 })
                .start();
            // 播放主体动画
            cc.tween(this.main)
                .to(this.animTime * this.timeScale, { scale: 1 }, { easing: CCEasing.elasticInOut })
                .call(() => {
                    resolve();
                })
                .start();
        })
    }




    public async beforeClose() {
        return new Promise((resolve, reject) => {
            cc.tween(this.background)
                .delay(this.animTime * 0.2 * this.timeScale)
                .to(this.animTime * 0.8 * this.timeScale, { opacity: 0 })
                .call(() => {
                    this.background.active = false;
                })
                .start();
            // 播放主体动画
            cc.tween(this.main)
                .to(this.animTime * this.timeScale, { scale: 0 }, { easing: 'backIn' })
                .call(() => {
                    resolve()
                })
                .start();
        })
    }

    private onCloseBtnClick() {
        this.hide();
    }

}

